package com.h3c.ptability.utils;

import cn.hutool.core.io.FileUtil;
import cn.hutool.json.JSONUtil;
import cn.hutool.system.OsInfo;
import cn.hutool.system.SystemUtil;
import com.aspose.words.*;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author chenyifan
 * @date 2022/5/20 16:38
 */
@Slf4j
public class AsposeUtil {

    /**
     * 加载license 用于破解 不生成水印
     *
     * @author LCheng
     * @date 2020/12/25 13:51
     */
    @SneakyThrows
    private static void getLicense() {
        try (InputStream is = AsposeUtil.class.getClassLoader().getResourceAsStream("License.xml")) {
            License license = new License();
            license.setLicense(is);
        }
    }

    /**
     * word转pdf
     *
     * @param wordPath    word文件保存的路径
     * @param pdfPathFile 转换后pdf文件
     * @author LCheng
     * @date 2020/12/25 13:51
     */
    @SneakyThrows
    public static void wordToPdf(String wordPath, File pdfPathFile) {
        getLicense();
        BufferedOutputStream os = FileUtil.getOutputStream(pdfPathFile);
        BufferedInputStream inputStream = FileUtil.getInputStream(wordPath);
        Document doc = new Document(inputStream);
        doc.save(os, SaveFormat.PDF);
        os.close();
        inputStream.close();
    }

}